local biz_name = "vb"
-- configure redis connector 
ConfigRedisConnectors[biz_name]={
    host = "127.0.0.1",
    -- cur: host = "192.168.2.28"
    port = 6379,
    unixpath = "/etc/redis/unixpath/redis.sock",
    db = NIL,
    user = NIL,
    passwd = NIL,
    queue = "queue_4"
}

local function _vb()
  return coroutine.create(function ()
    
    -- constants
    local REDIS_SEPERATOR = "\t"
    local NUM_SEMICOLON = 11
    local DEF_LOCATION = "0"
    local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
    local hget_cmd_format = "hget "..ConfigRedisConnectors[config.HGET_MOBILE_REDIS].queue.." "

    -- functions
    local local_config = config
    local local_reply = reply
    local local_request = request
    local local_luredis = luredis
    local os_time = os.time    
    -- data from requests
        -- tables
    local paraTable, headerTable, cookieString, cookieTable
        -- variables
    local create_time, ip, user_agent, guid, geoid, create_cookie, referer, mosv, path    
    -- temp variables
    local newcookie, reply_type, region, expire_time      
    local mbl_service_ 

    while true do	
	while true do -- validate params 
          local_reply.pushHeader(local_config.SERVER_HEADER, local_config.http.servername)
          local_reply.pushHeader(local_config.CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
          local_reply.pushHeader(local_config.PRAGMA_HEADER, "no-cache")
          local_reply.pushHeader(local_config.EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
          local_reply.pushHeader(local_config.CONTENT_TYPE_HEADER, "image/gif")
          local_reply.pushHeader(local_config.LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
          local_reply.pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
          local_reply.pushHeader("Content-Length", "0") -- error if not set Content-length
          local_reply.setStatus(local_reply.status_type.ok)
          paraTable = local_request.getParameters_value(); -- default seperator = ";"
          if #paraTable == NUM_SEMICOLON+1 then -- table length = number of seperators + 1
	    break;
          end
          print("Invalid params")
	  coroutine.yield(); -- return due to incorrect number of params
        end

	-- table        
        headerTable = local_request.getHeaders_key_value();

        user_agent = headerTable[local_config.USER_AGENT_HEADER]
        referer = headerTable[local_config.REFERER_HEADER];
	-- cookie 
        cookieString = headerTable[local_config.COOKIE_HEADER];
        cookieTable = parseCookies(cookieString);

        -- Get guid: guid
        guid = cookieTable[local_config.GUID_COOKIE];
        create_cookie = cookieTable[local_config.CREATE_COOKIE]


        newcookie = {};

        create_time = os_time();
        ip = local_request.getIP()

        if not guid then
            guid = create_guid(create_time, ip);
            table.insert(newcookie,local_config.GUID_COOKIE..guid)
        end
        -- get region: geoid
        geoid = cookieTable[local_config.REGION_COOKIE];
        if not geoid then -- ask redis for region            
            reply_type, region = util.redis_command(local_config.HGET_MOBILE_REDIS,false,
						    local_luredis.send_cmd,hget_cmd_format..c_inet_network(ip));
            if reply_type ~= local_luredis.reply_type.string then
	      region = DEF_LOCATION
	    else
              table.insert(newcookie,local_config.REGION_COOKIE..geoid)
	    end
        end
        -- get mobile os version (mosv)
        mosv = cookieTable[local_config.OS_MOBILE_COOKIE]
        if not mosv then -- ask mobile service for os
	    mbl_service_ = getMobileService();
	    if(mbl_service_ ~= nil) then
		osVersion = mbl_service_:parse_user_agent(userAgent)
                table.insert(newcookie,local_config.OS_MOBILE_COOKIE..mosv)
	    end
	end

	path = paraTable[NUM_SEMICOLON+1]
        paraTable[NUM_SEMICOLON+1] = nil -- path is set aside

        local_luredis.clear_cmd_buffer()
        local_luredis.append_cmd_buffer(
	  create_time, REDIS_SEPERATOR, ip, REDIS_SEPERATOR, "0", REDIS_SEPERATOR, 
          guid, REDIS_SEPERATOR, create_cookie, REDIS_SEPERATOR, geoid,REDIS_SEPERATOR,
	  table.concat(paraTable,";"),REDIS_SEPERATOR,path,REDIS_SEPERATOR,
	  referer, REDIS_SEPERATOR, user_agent)        
        
        util.redis_command(biz_name,true,local_luredis.send_cmd_buffer,lpush_cmd_format);

        -- set reply cookie
        expire_time = local_config.EXPIRE_TIME+create_time;
	table.insert(newcookie,"expires="..expire_time)
	table.insert(newcookie, "path=/")
        table.insert(newcookie,  "domain"..local_config.http.mobilecookie.domain)
        local_reply.pushHeader(local_config.SET_COOKIE_HEADER,table.concat(newcookie,";"))
        coroutine.yield()        
    end
  end)
end
f[biz_name] = _vb()
